home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-68k-src / machines / amiga68k / libsrc / stdlib / malloc.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  959b  |  48 lines

  1. /*  verwendet die Amiga-Pooled-Routinen     */
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #include <clib/alib_protos.h>
  7.  
  8. size_t _nalloc=16384;
  9. void *_first_mlist=0;
  10.  
  11. void _freemem(void)
  12. /*  Gibt allen Speicher frei    */
  13. {
  14.     if(_first_mlist) LibDeletePool(_first_mlist);
  15. }
  16. void *malloc(size_t nbytes)
  17. {
  18.     size_t *p;
  19.     if(!nbytes) return(0);
  20.     if(!_first_mlist) _first_mlist=LibCreatePool(0,_nalloc,_nalloc);
  21.     if(!_first_mlist) return(0);
  22.     p=LibAllocPooled(_first_mlist,nbytes+sizeof(size_t));
  23.     if(!p) return(0);
  24.     *p=nbytes;
  25.     return(p+1);
  26. }
  27. void free(void *ap)
  28. {
  29.     size_t *p=ap;
  30.     if(!p||!_first_mlist) return;
  31.     p--;
  32.     LibFreePooled(_first_mlist,p,*p+sizeof(size_t));
  33. }
  34.  
  35. void *realloc(void *old,size_t nsize)
  36. {
  37.     size_t osize;void *new;
  38.     if(!old) return(malloc(nsize));
  39.     osize=*((size_t *)old-1);
  40.     if(new=malloc(nsize)){
  41.         memcpy(new,old,osize>nsize ? nsize:osize);
  42.         free(old);
  43.     }
  44.     return(new);
  45. }
  46.  
  47.  
  48.